#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#define N 10000

int main()
{
        double *vector = (double *)malloc(sizeof(double) * N);
        double *result = (double *)malloc(sizeof(double) * N);
        double **matrix = (double **)malloc(sizeof(double *) * N);
        int i, j;
        struct timeval t_start, t_end;

        // 生成矩阵
        for (i = 0; i < N; i++)
                matrix[i] = (double *)malloc(sizeof(double) * N);

        for (i = 0; i < N; i++)
                for (j = 0; j < N; j++)
                        matrix[i][j] = 1.0 * rand() / RAND_MAX;

        // 生成向量
        for (i = 0; i < N; i++)
                vector[i] = 1.0 * rand() / RAND_MAX;

        // 计算矩阵乘向量
        gettimeofday(&t_start, NULL);
        for (i = 0; i < N; i++) {
                result[i] = 0;
                for (j = 0; j < N; j++)
                        result[i] += matrix[i][j] * vector[j];
        }
        gettimeofday(&t_end, NULL);

        // 输出结果
        for (i = 0; i < N; i++) {
                printf("%lf\n", result[i]);
        }
        fprintf(stderr, "Elapsed time: %lfs\n", (t_end.tv_sec - t_start.tv_sec) + 1e-6 * (t_end.tv_usec - t_start.tv_usec));

        free(vector);
        free(result);
        for (i = 0; i < N; i++) free(matrix[i]);
        free(matrix);
        return 0;
}
